package com.katze.common.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot3.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Collections;

@AutoConfigureBefore(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(prefix = "spring.datasource.druid", name = "enable", havingValue = "true", matchIfMissing = true)
public class DruidConfiguration {

    @Bean(initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DruidDataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();

        // 执行多条sql语句
        WallFilter wallFilter=new WallFilter();
        wallFilter.setConfig(new WallConfig());
        wallFilter.getConfig().setMultiStatementAllow(true);
        dataSource.setProxyFilters(Collections.singletonList(wallFilter));
        return dataSource;
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.datasource.druid.stat-view-servlet", name = "enabled", havingValue = "true")
    public ServletRegistrationBean<StatViewServlet> druidServlet(DataSource dataSource, DruidStatProperties druidStatProperties) throws Exception {// 主要实现web监控的配置处理
        String username = "admin", password = "admin";
        if (dataSource instanceof DruidDataSource){
            username = ((DruidDataSource) dataSource).getUsername();
            password = ((DruidDataSource) dataSource).getPassword();
            // 在创建 DataSource Bean时有调用init()，则DruidDataSource会自动解析密码
//            Properties properties = ((DruidDataSource) dataSource).getConnectProperties();
//            String decrypt = properties.getProperty("config.decrypt", "false");
//            if (Boolean.parseBoolean(decrypt)){
//                password = ConfigTools.decrypt(properties.getProperty("config.decrypt.key"), password);
//            }
        }
        DruidStatProperties.StatViewServlet servlet = druidStatProperties.getStatViewServlet();
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet());
        servletRegistrationBean.addUrlMappings(servlet.getUrlPattern() != null? servlet.getUrlPattern():"/actuator/druid/*");
        servletRegistrationBean.addInitParameter("loginUsername", username);//用户名
        servletRegistrationBean.addInitParameter("loginPassword", password);//密码
        servletRegistrationBean.addInitParameter("resetEnable", servlet.getResetEnable() != null? servlet.getResetEnable():"false");//是否能够重置数据源
        if (servlet.getDeny() != null) servletRegistrationBean.addInitParameter("deny", servlet.getDeny());//黑名单
        servletRegistrationBean.addInitParameter("allow", servlet.getAllow() != null? servlet.getAllow():"127.0.0.1");//白名单
        return servletRegistrationBean;

    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.datasource.druid.web-stat-filter", name = "enabled", havingValue = "true")
    public FilterRegistrationBean<WebStatFilter> filterRegistrationBean(DruidStatProperties druidStatProperties) {
        DruidStatProperties.WebStatFilter webStatFilter = druidStatProperties.getWebStatFilter();
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns(webStatFilter.getUrlPattern() != null? webStatFilter.getUrlPattern():"/*");//全部请求进行监控处理
        filterRegistrationBean.addInitParameter("exclusions", webStatFilter.getExclusions() != null? webStatFilter.getExclusions():"/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");//排除
        return filterRegistrationBean;
    }
}
